import { Component } from '@angular/core';
import {NzColDirective, NzRowDirective} from 'ng-zorro-antd/grid';
import {NzButtonComponent} from 'ng-zorro-antd/button';
import {NzTooltipDirective} from 'ng-zorro-antd/tooltip';
import {NzInputDirective} from 'ng-zorro-antd/input';
import { NzSpaceModule } from 'ng-zorro-antd/space';
import {FormsModule} from '@angular/forms';
import {RedisService} from '../../services/redis.service';
import {NzFormDirective} from 'ng-zorro-antd/form';
import {NzSpinComponent} from 'ng-zorro-antd/spin';
import {NgIf} from '@angular/common';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'app-lock',
  standalone: true,
  imports: [
    NzRowDirective,
    NzButtonComponent,
    NzTooltipDirective,
    NzColDirective,
    NzInputDirective,
    NzSpaceModule,
    FormsModule,
    NzFormDirective,
    NzSpinComponent,
    NgIf
  ],
  templateUrl: './lock.component.html',
  styleUrl: './lock.component.css'
})
export class LockComponent {

  constructor(private redisService: RedisService,
              private message: NzMessageService) {}

  redisKey:string = "test";

  normalLoading = false;
  limitLoading = false;
  lockLoading=false;
  tryLoading=false;
  tryTimeLoading=false;

  normal(){
    this.normalLoading = true;
    this.redisService.normal(this.redisKey).subscribe(res => {
      this.normalLoading = false;
    })

    this.lockLoading = true;
    this.redisService.lock(this.redisKey).subscribe(res =>{
      this.lockLoading = false;
    })
  }

  limit(){
    this.limitLoading = true;
    this.redisService.limit(this.redisKey).subscribe(res => {
      this.limitLoading = false;
    })

    this.lockLoading = true;
    this.redisService.lock(this.redisKey).subscribe(res =>{
      this.lockLoading = false;
    })
  }

  tryLock(){
    this.tryLoading = true;
    this.redisService.tryLock(this.redisKey).subscribe(res => {
      this.tryLoading = false;
      if(res){
        this.message.success("获取锁成功！！！！");
      }else{
        this.message.error("获取锁失败！！！")
      }
    })
  }

  tryTimeLock(){
    this.tryTimeLoading = true;
    this.redisService.tryLockTime(this.redisKey).subscribe(res => {
      this.tryTimeLoading = false;
      if(res){
        this.message.success("获取锁成功！！！！");
      }else{
        this.message.error("获取锁失败！！！")
      }
    })
  }

}
